﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCCommon.OAEnums;
using BCEntity.OA.TeamWorks.TaskRecord;
using BCCommon;
namespace BCData.OA.TeamWorks.TaskReceiverRecord
{
    public class TaskReceiverRecordData : ITaskReceiverRecordData
    {
        private const string QUERY = @"SELECT TaskReceiverRecordId,WorkflowActivityInstanceId,TaskStep,TaskRecordNo,ReceiverAdminId,ReceiverAdminFullName,ReceiverRoleType,ReceiverRole,CreateTime FROM OA_TaskReceiverRecord";

        private readonly IDatabaseContext databaseContext;

        public TaskReceiverRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<IEnumerable<TaskReceiverRecordEntity>> GetByWorkflowActivityInstanceId(long workflowActivityInstanceId)
        {
            string sql = string.Format("{0} Where WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY);

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<TaskReceiverRecordEntity>(sql, parameters);
        }

        public async Task<bool> Insert(List<TaskReceiverRecordEntity> taskReceiverRecordEntities)
        {
            List<string> sqls = new List<string>();
            List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();
            foreach (var item in taskReceiverRecordEntities)
            {
                string sql = SqlCommands.GetInsertSql("OA_TaskReceiverRecord",
                    new string[] { "WorkflowActivityInstanceId", "TaskStep", "TaskRecordNo", "ReceiverAdminId", "ReceiverAdminFullName", "ReceiverRole", "CreateTime" });

                DynamicParameters parameters = new DynamicParameters();

                parameters.Add("?WorkflowActivityInstanceId", item.WorkflowActivityInstanceId, DbType.Int64);
                parameters.Add("?TaskStep", item.TaskStep, DbType.Int32);
                parameters.Add("?TaskRecordNo", item.TaskRecordNo, DbType.AnsiString, size: 50);
                parameters.Add("?ReceiverAdminId", item.ReceiverAdminId, DbType.Int64);
                parameters.Add("?ReceiverAdminFullName", item.ReceiverAdminFullName, DbType.AnsiString);
                parameters.Add("?ReceiverRole", item.ReceiverRole.ToString(), DbType.AnsiString);
                parameters.Add("?CreateTime", item.CreateTime, DbType.DateTime);

                sqls.Add(sql);

                dynamicParameters.Add(parameters);
            }
            return await this.databaseContext.Master.TransactionExecuteAsync(sqls, dynamicParameters, databaseContext.Transaction) > 0;
        }

        public async Task<IEnumerable<TaskReceiverRecordEntity>> GetByReceiverAdminIdAndReceiverRoleAsync(long receiverAdminId, OATaskReceiverRole oATaskReceiverRole)
        {
            string sql = string.Format("{0} WHERE ReceiverAdminId=?ReceiverAdminId AND ReceiverRole = ?ReceiverRole", QUERY);

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ReceiverAdminId", receiverAdminId, DbType.Int64);
            parameters.Add("?ReceiverRole", oATaskReceiverRole.ToString(), DbType.AnsiString);
            return await this.databaseContext.Slave.QueryAsync<TaskReceiverRecordEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TaskReceiverRecordEntity>> GetByWorkflowActivityInstanceIdsAndReceiverRoleAsync(List<long> workflowActivityInstanceIds, OATaskReceiverRole oATaskReceiverRole)
        {
            string sql = string.Format("{0} WHERE FIND_IN_SET(WorkflowActivityInstanceId,?WorkflowActivityInstanceIds) AND ReceiverRole = ?ReceiverRole GROUP BY ReceiverAdminId", QUERY);

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceIds", string.Join(",", workflowActivityInstanceIds), DbType.AnsiString);
            parameters.Add("?ReceiverRole", oATaskReceiverRole.ToString(), DbType.AnsiString);
            return await this.databaseContext.Slave.QueryAsync<TaskReceiverRecordEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TaskReceiverRecordEntity>> GetTaskReceiverRecordsAsync(string taskRecordNo)
        {
            string sql = $"{QUERY} WHERE TaskRecordNo = ?TaskRecordNo";

            return await this.databaseContext.Slave.QueryAsync<TaskReceiverRecordEntity>(sql, new { taskRecordNo });
        }
    }
}
